package io.github.hadyang.leetcode.offer;

import org.junit.Test;

/** @author haoyang.shi */
public class LastRemaining {

  @Test
  public void test() {
    System.out.println(LastRemaining_Solution(5, 3));
  }

  public int LastRemaining_Solution(int n, int m) {
    if (n == 0) return -1;

    if (n == 1) return 0;

    int last = 0;
    for (int i = 2; i <= n; i++) {
      last = (last + m) % i;
    }

    return last;
  }
}
